package com.buyhouse.zhaimao.activity;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Toast;
import com.buyhouse.zhaimao.MyApplication;
import com.buyhouse.zhaimao.R;
import com.buyhouse.zhaimao.fragment.MenuFragment;
import com.buyhouse.zhaimao.fragment.MenuFragment.SLMenuListClickListener;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;

/**
 * 主页
 * 
 * @author zhouby
 *
 */
public class MainActivity extends SlidingActivity implements 
		SLMenuListClickListener{
	//	public class MainActivity extends SlidingFragmentActivity {
	
	private SlidingMenu mSlidingMenu;
		
	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MyApplication.getInstance().addActivity(this);
		setContentView(R.layout.frame_content);
		initUI();
		initData();	
	}
	
	
	private void initUI(){
//		FragmentBean fragmentBean=new FragmentBean();
//		fragmentBean.setFragment(new NearbyExpertFragment());
//		fragmentBean.setId(0);
//		fragmentList.add(fragmentBean);
	}
	
	private void initData(){
//		setTitle("Home");
//		setTitle(R.string.sliding_title);
       
        //1、 set the Behind View
        setBehindContentView(R.layout.frame_menu);
      
        // customize the SlidingMenu
        mSlidingMenu = getSlidingMenu();
        mSlidingMenu.setMode(SlidingMenu.LEFT);
//      mSlidingMenu.setMenu(R.layout.frame_menu);	//设置左侧菜单的布局文件
//      mSlidingMenu.setSecondaryMenu(R.layout.frame_menu);	设置右侧菜单的布局文件
        
//      mSlidingMenu.setShadowWidth(5);
//      mSlidingMenu.setBehindOffset(100);
//      mSlidingMenu.setShadowDrawable(R.drawable.drawer_shadow);//设置阴影图片
//      mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width); //设置阴影图片的宽度
//      mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset); //SlidingMenu划出时主页面显示的剩余宽度
        mSlidingMenu.setBehindWidthRes(R.dimen.slidingmenu_offset);
        mSlidingMenu.setFadeDegree(0.35f);
        //设置SlidingMenu 的手势模式
        //TOUCHMODE_FULLSCREEN 全屏模式，在整个content页面中，滑动，可以打开SlidingMenu
        //TOUCHMODE_MARGIN 边缘模式，在content页面中，如果想打开SlidingMenu,你需要在屏幕边缘滑动才可以打开SlidingMenu
        //TOUCHMODE_NONE 不能通过手势打开SlidingMenu
        mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        //设置 SlidingMenu 内容
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();    
//        MenuFragment menuFragment = new MenuFragment();
        fragmentTransaction.replace(R.id.menu, new MenuFragment());
//        fragmentTransaction.replace(R.id.content, new NearbyExpertFragment());
//        fragmentTransaction.add(R.id.content, new NearbyExpertFragment(), "NearbyExpert");
//        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
        
        //使用左上方icon可点，这样在onOptionsItemSelected里面才可以监听到R.id.home
//        getActionBar().setDisplayHomeAsUpEnabled(true);
	}
   
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            
            toggle(); //动态判断自动关闭或开启SlidingMenu
//          getSlidingMenu().showMenu();//显示SlidingMenu
//          getSlidingMenu().showContent();//显示内容
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private long firstTime = 0; // 判断是否推出
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			long secondTime = System.currentTimeMillis();
			if (secondTime - firstTime > 2000) { // 如果两次按键时间间隔大于2秒，则不退出
				Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
				firstTime = secondTime;// 更新firstTime
				return true;
			} else { // 两次按键小于2秒时，退出应用
				MyApplication.getInstance().exit();
			}
			break;
		}
		return super.onKeyUp(keyCode, event);
	}


	@Override
	public void selectListener() {
		
		toggle();
	}


	@Override
	public void selectId(int id, Fragment fragmenManager) {
		
		
	}






	
	
}
